layui.define(['admin','table','form','layarea','element','laydate'],function (exports){
    let $ = layui.$
        ,admin  = layui.admin
        ,view   = layui.view
        ,table = layui.table
        ,form = layui.form
        ,layarea = layui.layarea
        ,element = layui.element
        ,laydate = layui.laydate

    let model = 'coupon'
        ,module = ['list','record','use']

    const active = {

        list_form:function (d) {
            admin.popup({
                title: '卡券编辑'
                ,area: ['500px', '500px']
                ,id: 'coupon-list-table'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[0]+'_form', d).done(function(){

                        let time = new Date()
                        laydate.render({
                            elem: '#form-start-date'
                            //,value: time.getFullYear()+'-'+(time.getMonth() < 9 ? '0' : '')+(time.getMonth()+1)+'-'+(time.getDate() < 10 ? '0' : '')+time.getDate()
                        });
                        laydate.render({
                            elem: '#form-end-date'
                            //,value: time.getFullYear()+'-'+(time.getMonth() < 9 ? '0' : '')+(time.getMonth()+1)+'-'+(time.getDate() < 10 ? '0' : '')+time.getDate()
                        });

                        form.render(null, 'layuiadmin-app-form-list');

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'coupon/list/post'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('LAY-coupon-list'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        }
    }

    let table_id_0 = model+'-'+module[0]
        ,table_path_0 = model+'/'+module[0]

    table.render({
        elem: '#LAY-'+table_id_0
        ,toolbar: '#table-tool'
        ,url: '/api/admins/'+table_path_0
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'name', title: '名称'}
            ,{field: 'value',width: 170, title: '价值'}
            ,{field: 'start',width: 200, title: '开始时间'}
            ,{field: 'end',width: 200, title: '结束时间'}
            ,{field: 'expire',title: '有效天数',width: 100}
            ,{field: 'type',title: '优惠券类型',templet: '#table-type'}
            ,{title: '操作', width: 80, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    //头部工具栏
    table.on('toolbar(LAY-'+table_id_0+')', function(obj){

        switch(obj.event){
            case 'add':
                active.list_form();
                break;
        };
    });

    table.on('tool(LAY-'+table_id_0+')', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){

        } else if(obj.event === 'edit'){
            active.list_form(data);
        }
    });

    //监听状态工具栏
    form.on('switch(table-'+table_id_0+'-status)', function(obj){
        let data = JSON.parse(decodeURIComponent($(this).data('json')));

        admin.req({
            url: table_path_0+'/admin'
            ,data: {id:data.id,status:obj.elem.checked ?1:0}
        });
    });

    let table_id_1 = model+'-'+module[1]
        ,table_path_1 = model+'/'+module[1]

    active.record_form = function(d){
        admin.popup({
            title: '卡券发放'
            ,area: ['500px', '400px']
            ,id: 'coupon-record-table'
            ,resize: false
            ,success: function(layero, index){
                view(this.id).render(model+'/form/'+module[1]+'_form', d).done(function(){

                    form.render(null, 'layuiadmin-app-form-list');

                    //监听提交
                    form.on('submit(layuiadmin-app-form-submit)', function(data){
                        var field = data.field; //获取提交的字段

                        admin.req({
                            url:'coupon/record/post'
                            ,type: 'post'
                            ,data:field
                            ,done:function (res) {

                                layui.table.reload('LAY-coupon-record'); //重载表格
                                layer.close(index); //执行关闭
                            }
                        });
                        return false;
                    });

                });
            }
        });
    }

    table.render({
        elem: '#LAY-'+table_id_1
        ,toolbar: '#table-tool'
        ,url: '/api/admins/'+table_path_1
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'name', title: '姓名'}
            ,{field: 'mobile', title: '手机号'}
            ,{field: 'value',width: 170, title: '价值'}
            ,{field: 'deadline',width: 200, title: '到期时间'}
            ,{field: 'type',title: '优惠券类型',templet: '#table-type'}
            ,{field: 'created_at',title: '领取时间'}
            //,{title: '操作', width: 80, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    //头部工具栏
    table.on('toolbar(LAY-'+table_id_1+')', function(obj){

        switch(obj.event){
            case 'add':
                active.record_form();
                break;
        };
    });


    let table_id_2 = model+'-'+module[2]
        ,table_path_2 = model+'/'+module[2]

    table.render({
        elem: '#LAY-'+table_id_2
        ,toolbar: '#table-tool'
        ,url: '/api/admins/'+table_path_2
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'name', title: '姓名'}
            ,{field: 'mobile', title: '手机号'}
            ,{field: 'amount',width: 170, title: '消费金额'}
            ,{field: 'price',width: 200, title: '优惠金额'}
            ,{field: 'admin_name',title: '管理员'}
            ,{field: 'desc',title: '使用描述'}
            ,{field: 'created_at',title: '使用时间'}
            //,{title: '操作', width: 80, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    exports('coupon',active);
});
